package vip.xiaonuo.biz.modular.pay.util.wxpay;

import jakarta.annotation.PostConstruct;
import lombok.Getter;
import lombok.Setter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import vip.xiaonuo.biz.modular.pay.entity.BizPayAccount;
import vip.xiaonuo.biz.modular.pay.util.bankpay.PayClient;

import java.io.*;

/**
 * ClassName:WXPayClient（微信支付客户端）
 * Description:
 *
 * @Author:薛守立
 * @CreateTime:2024/12/2 13:24
 */
@Getter
@Setter
public class WXPayClient extends WXPayConfig {
    private static Logger logger = LoggerFactory.getLogger(WXPayClient.class);

    /** 支付请求的账户信息 */
    private BizPayAccount bizPayAccount;

    /** 支付接口的回调接口 */
    private final static String CALLBACK_URL = "";

    /** 支付证书的二进制 */
    private byte[] certData;

    public WXPayClient(BizPayAccount bizPayAccount) throws IOException {
        this.bizPayAccount = bizPayAccount;
    }

    /**
     * 参数初始化
     * @throws Exception
     */
    @PostConstruct
    public void init() throws Exception{
        String certPath = this.bizPayAccount.getWxCertPath();
        File file=new File(certPath);
        FileInputStream in=new FileInputStream(file);
        BufferedInputStream bin=new BufferedInputStream(in);
        this.certData=new byte[(int)file.length()];
        bin.read(this.certData);
        bin.close();
        in.close();
    }

    @Override
    String getAppID() {
        return this.bizPayAccount.getAppId();
    }

    @Override
    String getMchID() {
        return this.bizPayAccount.getMchId();
    }

    @Override
    public String getKey() {
        return this.bizPayAccount.getWxKey();
    }

    @Override
    InputStream getCertStream() {
        ByteArrayInputStream in=new ByteArrayInputStream(this.certData);
        return in;
    }

    @Override
    IWXPayDomain getWXPayDomain() {
        return new IWXPayDomain() {
            @Override
            public void report(String domain, long elapsedTimeMillis, Exception ex) {

            }

            @Override
            public DomainInfo getDomain(WXPayConfig config) {
                return new DomainInfo(WXPayConstants.DOMAIN_API,true);
            }
        };
    }
}
